<?php 
class Md_Category extends WdkModel
{
	public function __construct()
	{
		parent::__construct();
		$this->_init();
	}
	
	private function _init()
	{
		$this->table	= 'category';
	}
	
	/**
	 *	Select query for Classes..
	 *  @author	kinuz
	 *  @since	2012. 04. 18
	 */
	public function cLoad($category_seq)
	{
		return $this->getData("*", "`category_seq` = {$this->escape($category_seq)}");
	}
	
	/**
	 *	Select query for Classes..
	 *  @author	kinuz
	 *  @since	2012. 04. 18
	 */
	public function cLoadByCode($category_code)
	{
		return $this->getData("*", "`category_code` = '{$this->escape($category_code)}'");
	}
	
	/**
	 *	Select query for Classes..
	 *  @author	kinuz
	 *  @since	2012. 04. 18
	 */
	public function nextCode($parent_code = '')
	{
		$cate = Wdk::helper('Category');
		try 
		{
			$data = $this->getData("MAX(`category_code`) AS `category_code`", "`category_code` LIKE '{$this->escape($parent_code)}__'");
			$code = substr($data['category_code'], -2);
		}
		catch(NoRecordException $e)
		{
			$code = '';
		}
		return $parent_code.$cate->next($code);
	}
	
	/**
	 *	Select query for Classes..
	 *  @author	kinuz
	 *  @since	2012. 04. 18
	 */
	public function makeTemp($category_code = '')
	{
		$code = str_pad("",strlen($category_code), "-");
		$sql = sprintf(	"UPDATE `{$this->table}` SET  %s WHERE %s"
						, "`category_code` = CONCAT('$code', SUBSTRING(`category_code`, ".(strlen($category_code)+1)."))"
						, "`category_code` LIKE '{$category_code}%'"
					);
		$this->dbo->execute($sql);
	}
	
	/**
	 *	Select query for Classes..
	 *  @author	kinuz
	 *  @since	2012. 04. 18
	 */
	public function releaseTemp($category_code = '')
	{
		$code = str_pad("",strlen($category_code), "-");
		$sql = sprintf(	"UPDATE `{$this->table}` SET  %s WHERE %s"
						, "`category_code` = CONCAT('$category_code', SUBSTRING(`category_code`, ".(strlen($category_code)+1)."))"
						, "`category_code` LIKE '{$code}%'"
					);
		$this->dbo->execute($sql);
	}
	
	/**
	 *	Select query for Classes..
	 *  @author	kinuz
	 *  @since	2012. 04. 18
	 */
	public function moveUp($from = '', $to='')
	{
		$cate = Wdk::helper('Category');
		try
		{
			$lists = $this->getListAll("*", array("`category_code` BETWEEN '{$from}' AND '{$to}'", "LENGTH(`category_code`) = ".strlen($from)), '`category_code` ASC');
			$parent_code = substr($from, 0, -2);
			foreach($lists as $data)
			{
				$new_code = $cate->next(substr($data['category_code'], -2));
				$sql = sprintf(	"UPDATE `{$this->table}` SET  %s WHERE %s"
								, "`category_code` = CONCAT('{$parent_code}{$new_code}', SUBSTRING(`category_code`, ".(strlen($data['category_code'])+1)."))"
								, "`category_code` LIKE '{$data['category_code']}%'"
							);
				$this->dbo->execute($sql);
			}
		}
		catch(NoRecordException $e){}
	}
	
	/**
	 *	Select query for Classes..
	 *  @author	kinuz
	 *  @since	2012. 04. 18
	 */
	public function moveDown($from = '', $to='')
	{
		$cate = Wdk::helper('Category');
		try
		{
			$lists = $this->getListAll("*", array("`category_code` BETWEEN '{$from}' AND '{$to}'", "LENGTH(`category_code`) = ".strlen($from)), '`category_code` ASC');
			$parent_code = substr($from, 0, -2);
			foreach($lists as $data)
			{
				$new_code = $cate->prev(substr($data['category_code'], -2));
				$sql = sprintf(	"UPDATE `{$this->table}` SET  %s WHERE %s"
								, "`category_code` = CONCAT('{$parent_code}{$new_code}', SUBSTRING(`category_code`, ".(strlen($data['category_code'])+1)."))"
								, "`category_code` LIKE '{$data['category_code']}%'"
							);
				$this->dbo->execute($sql);
			}
		}
		catch(NoRecordException $e){}
	}
}